package org.example;

import org.elasticsearch.action.delete.DeleteRequestBuilder;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequestBuilder;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.update.UpdateRequestBuilder;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.Test;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;

    public class ESTest {

    //创建ES客户端
    public TransportClient createClient(){
        try {
            TransportClient client = new PreBuiltTransportClient(Settings.EMPTY)
                    .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));
            return client;
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        return null;
    }

    //创建索引，添加文档
    @Test
    public void testAdd(){
        //创建ES客户端
        TransportClient client = createClient();

        //创建索引
        IndexRequestBuilder indexRequestBuilder = client.prepareIndex("mypethome", "student", "2");

        Map<String, Object> data = new HashMap<>();
        data.put("id", 1L);
        data.put("name", "zs");
        data.put("sex", 1);
        data.put("age", 18);

        //添加数据
        IndexResponse indexResponse = indexRequestBuilder.setSource(data).get();

        System.out.println(indexResponse);
    }

        //修改
        @Test
        public void testUpdate(){
            //创建ES客户端
            TransportClient client = createClient();

            //创建索引
            UpdateRequestBuilder updateRequestBuilder = client.prepareUpdate("mypethome", "student", "1");

            Map<String, Object> data = new HashMap<>();
            data.put("id", 1);
            data.put("name", "ls");
            data.put("sex", 0);
            data.put("age", 19);

            //添加数据
            UpdateResponse updateResponse = updateRequestBuilder.setDoc(data).get();

            System.out.println(updateResponse);
        }

        //删除数据
        @Test
        public void testDelete(){
            //创建ES客户端
            TransportClient client = createClient();

            //创建索引
            DeleteRequestBuilder deleteRequestBuilder = client.prepareDelete("mypethome", "student", "1");

            //删除数据
            DeleteResponse deleteResponse = deleteRequestBuilder.get();

            System.out.println(deleteResponse);
        }

        //获取数据
        @Test
        public void testQuery(){
            //创建ES客户端
            TransportClient client = createClient();

            //创建索引
            GetRequestBuilder getRequestBuilder = client.prepareGet("mypethome", "student", "1");

            //获取数据
            GetResponse getResponse = getRequestBuilder.get();

//        System.out.println(getResponse);//直接打印出来会报错，因为调用toString方法时，里面的内容无法转成JSON数据
            System.out.println(getResponse.getSource());
        }
}